package interfaces

import (
	"bytes"
	"github.com/gin-gonic/gin"
	"io/ioutil"
	"net/http"
	"ruoyi/application"
	"ruoyi/domain/entity/common"
	"ruoyi/domain/entity/response"
	"ruoyi/domain/entity/system"
	"ruoyi/utils"
	"time"
)

type Menu struct {
	menu application.MenuAppInterface
	user application.UserAppInterface
	role application.RoleAppInterface
}

//LoginService constructor
func NewMenu(menu application.MenuAppInterface, user application.UserAppInterface, role application.RoleAppInterface) *Menu {
	return &Menu{
		menu: menu,
		user: user,
		role: role,
	}
}

// GetRoutersHandler /*
func (m *Menu) GetRoutersHandler(context *gin.Context) {
	userId, _ := context.Get("userId")
	m.user.FindUserById(userId)

	menu := m.menu.SelectMenuTreeByUserId(utils.GetInterfaceToInt(userId))
	var data = m.menu.BuildMenus(menu)
	context.JSON(http.StatusOK, gin.H{
		"msg":  "操作成功",
		"code": http.StatusOK,
		"data": data,
	})
}

func (m *Menu) ListMenu(context *gin.Context) {

	menuName := context.DefaultQuery("menuName", "")
	status := context.DefaultQuery("status", "")

	var param = common.SearchTableDataParam{
		Other: system.SysMenu{
			MenuName: menuName,
			Status:   status,
		},
		Params: common.Params{},
	}
	userId, _ := context.Get("userId")
	if m.user.IsAdminById(utils.GetInterfaceToInt(userId)) {
		var result = m.menu.SelectSysMenuList(param)
		context.JSON(http.StatusOK, gin.H{
			"msg":  "操作成功",
			"code": http.StatusOK,
			"data": result,
		})
	} else {
		var result = m.menu.SelectSysMenuListByUserId(int(userId.(float64)), param)
		context.JSON(http.StatusOK, gin.H{
			"msg":  "操作成功",
			"code": http.StatusOK,
			"data": result,
		})
	}
}

func (m *Menu) GetMenuInfo(context *gin.Context) {
	userId, _ := context.Get("userId")
	println(userId)
	var menuId = context.Param("menuId")
	var date = m.menu.FindMenuInfoById(menuId)
	context.JSON(http.StatusOK, gin.H{
		"msg":  "操作成功",
		"code": http.StatusOK,
		"data": date,
	})
}

func (m *Menu) GetTreeSelect(context *gin.Context) {
	userId, _ := context.Get("userId")
	println(userId)
	data, _ := ioutil.ReadAll(context.Request.Body)
	// 这点很重要，把字节流重新放回 body 中
	context.Request.Body = ioutil.NopCloser(bytes.NewBuffer(data))
	var menuParam system.SysMenu
	if err := context.ShouldBind(&menuParam); err != nil {
		context.JSON(http.StatusOK, response.ReturnFailMsg("参数不能为空"))
		return
	}

	menu := m.menu.SelectMenuTree(utils.GetInterfaceToInt(userId), menuParam)
	var result = m.menu.BuildMenuTreeSelect(menu)

	context.JSON(http.StatusOK, gin.H{
		"msg":  "操作成功",
		"code": http.StatusOK,
		"data": result,
	})
}

func (m *Menu) TreeSelectByRole(context *gin.Context) {
	userId, _ := context.Get("userId")
	var roleId = context.Param("roleId")
	var menuPerms system.SysMenu
	menu := m.menu.SelectMenuTree(utils.GetInterfaceToInt(userId), menuPerms)
	var result = m.menu.BuildMenuTreeSelect(menu)
	roles := m.role.FindRoleInfoById(roleId)
	var checkedKeys = m.menu.SelectMenuListByRoleId(roleId, roles.MenuCheckStrictly)
	context.JSON(http.StatusOK, gin.H{
		"msg":         "操作成功",
		"code":        http.StatusOK,
		"menus":       result,
		"checkedKeys": checkedKeys,
	})
}

func (m *Menu) SaveMenu(context *gin.Context) {
	userId, _ := context.Get("userId")
	var menuParm system.SysMenu
	if err := context.ShouldBind(&menuParm); err != nil {
		context.JSON(http.StatusOK, response.ReturnFailMsg("参数不能为空"))
		return
	}
	user := m.user.FindUserById(userId)
	menuParm.CreateBy = user.UserName
	menuParm.CreateTime = time.Now()
	result := m.menu.AddMenu(menuParm)
	context.JSON(http.StatusOK, result)
}

func (m *Menu) UploadMenu(context *gin.Context) {
	userId, _ := context.Get("userId")
	var menuParm system.SysMenu
	if err := context.ShouldBind(&menuParm); err != nil {
		context.JSON(http.StatusOK, response.ReturnFailMsg("参数不能为空"))
		return
	}
	user := m.user.FindUserById(userId)
	menuParm.UpdateBy = user.UserName
	menuParm.UpdateTime = time.Now()
	result := m.menu.UpdateMenu(menuParm)
	context.JSON(http.StatusOK, result)
}

func (m *Menu) DeleteMenu(context *gin.Context) {
	var menuId = context.Param("menuId")
	result := m.menu.DeleteMenu(menuId)
	context.JSON(http.StatusOK, result)
}
